package com.maliankeji.mybatisflexdemo.controller;

import com.maliankeji.mybatisflexdemo.entity.UserInfo;
import com.maliankeji.mybatisflexdemo.service.UserInfoService;
import com.maliankeji.mybatisflexdemo.util.Result;
import com.maliankeji.mybatisflexdemo.util.Tools;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;

    @PostMapping("/add")
    public String addUser(@RequestBody UserInfo user) {
        int result = userInfoService.addUser(user);
        if (result == 1) return "success";
        else return "fail";
    }

    @PostMapping("/delete")
    public String delUser(@RequestBody UserInfo user) {
        int result = userInfoService.deleteUser(user);
        if (result == 1) return "success";
        else return "fail";
    }

    @PostMapping("/update")
    public String updateUser(@RequestBody UserInfo user) {
        int result = userInfoService.updateUser(user);
        if (result == 1) return "success";
        else return "fail";
    }

    @GetMapping("/list")
    public String getAllUser() {
        List<UserInfo> list = userInfoService.getAllUserInfo();
        Result result = new Result("200", "success", list);
        return Tools.toJSON(result);
    }

    @GetMapping("/get")
    public String getUserById(@RequestParam int id){
        UserInfo info = userInfoService.getUserInfoById(id);
        Result result = new Result("200", "success", info);
        return Tools.toJSON(result);
    }
}
